<?php
include '../../system/data/settings.php';
include '../../system/libraries/database.php';
include 'stats.inc.php';


$action = $_REQUEST['action'];
if ($action == 'get-data')
{
  if (empty($_GET['datefrom']))
    $dateFrom = date("Y-m-d").' 00:00:00';
  else
    $dateFrom = $_GET["datefrom"];

  
  if (empty($_GET['dateto']))
    $dateTo = date("Y-m-d").' 23:59:59';
  else
    $dateTo= $_GET["dateto"];

  if (empty($_GET['categoryId']))
    $categoryID = 0;
  else
    $categoryID= $_GET["categoryId"];

  if (empty($_GET['timeunit']))
    $timeUnit = 0;
  else
    $timeUnit= $_GET["timeunit"];

  buildData($dateFrom, $dateTo, $timeUnit);
}

/***  Get Fastest Growing Categories  ***/

if ($action == 'get-growth')
{
  if (empty($_GET['dateto']))
    $dateTo = date("Y-m-d").' 00:00:00';
  else
    $dateTo = $_GET["dateto"];

  print json_encode(growth($dateTo));
}

/***  Get Categories #category_selector ***/



function GetData($dateFrom, $dateTo, $timeUnit, $categoryID)
{
  $results = array();

  $TOTAL = 0;
  $dataMinim = array();
  $dataMaxim= array();
  
  $dataMinim = array('value'=> 20000);
  $dataMaxim = array('value'=> 0 );
  
  $columns = "itv.day_stamp AS `group_column`, 
  categories.name AS category_name, SUM(itv.views) AS `total`";
  
  $where = "itv.day_stamp>=(TO_DAYS('$dateFrom')-719528) AND 
  itv.day_stamp<=(TO_DAYS('$dateTo')-719528) AND 
  categories.id = $categoryID";
  
  $from = "item_daily_views AS itv INNER JOIN items ON itv.item_id = items.id
  INNER JOIN categories ON items.category_id = categories.id";
  
  $suffix = 'GROUP BY categories.id, `group_column`';
  $query = "SELECT $columns FROM $from  WHERE $where $suffix";
  $rows = ExecuteQuery($query);
  $curTimeRangeStart = $dateFrom;
  $curTimeRangeEnd = GetTimeRangeEnd($curTimeRangeStart, $timeUnit);
  $curTotalValue = 0;

  foreach ($rows as $key => $value)
  {
    $day = date("Y-m-d", $value['group_column']* 86400);
    
    while ($day > $curTimeRangeEnd)
    {
      $dataMaxim =  Maxim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMaxim, $timeUnit);
      $dataMinim =  Minim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMinim, $timeUnit);
      $results[] = array('range-start' => $curTimeRangeStart, 'range-end' => $curTimeRangeEnd, 'value' => $curTotalValue);
      
      $curTotalValue = 0;
      $curTimeRangeStart = GetNextTimeRangeStart($curTimeRangeStart, $timeUnit);
      $curTimeRangeEnd = GetTimeRangeEnd($curTimeRangeStart, $timeUnit);
    }
    $curTotalValue += $value['total'];
    $TOTAL += $value['total']; 
    
    
  }

  $endDay = $dateTo;

  while ($endDay > $curTimeRangeEnd)
  {
    $results[] = array('range-start' => $curTimeRangeStart, 'range-end' => $curTimeRangeEnd, 'value' => $curTotalValue);
    $curTotalValue = 0;
    $curTimeRangeStart = GetNextTimeRangeStart($curTimeRangeStart, $timeUnit);
    $curTimeRangeEnd = GetTimeRangeEnd($curTimeRangeStart, $timeUnit);
  }
  
  $dataMaxim =  Maxim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMaxim, $timeUnit);
  $dataMinim =  Minim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMinim, $timeUnit);
  
  $results[] = array('range-start' => strtotime($curTimeRangeStart), 'range-end' => strtotime($endDay), 'value' => $curTotalValue);
  $results['data'] = array('Min' =>$dataMinim, 'Max' =>$dataMaxim, 'Total' => $TOTAL);
  
  return $results; 
}


function buildData($dateFrom, $dateTo, $timeunit)
{
  $response = array();
  $categories = GetCategories($dateFrom, $dateTo);
       
  foreach ($categories as $key => $value)
    {
      $response[$value['category_name']] = GetData($dateFrom, $dateTo, $timeunit, $value['category_id'] );
    }
    print json_encode($response);
}

function growth($dateTo)
{
  
  $columns = "first.views, second.sec_views, second.sec_views/first.views as raport, first.cat_id, first.name";
  
  $firstQuery = "SELECT categories.id AS `cat_id`, SUM( item_daily_views.views ) AS views, categories.name as name 
    FROM item_daily_views INNER JOIN items ON item_daily_views.item_id = items.id INNER JOIN categories 
    ON items.category_id = categories.id WHERE item_daily_views.day_stamp >= ( TO_DAYS( '$dateTo') -719528 -56 ) 
    AND item_daily_views.day_stamp <= ( TO_DAYS( '$dateTo' ) -719528 -42 ) GROUP BY `cat_id`";

  
  $secondQuery = "SELECT categories.id AS `cat_id`, SUM( item_daily_views.views ) AS sec_views 
    FROM item_daily_views INNER JOIN items ON item_daily_views.item_id = items.id INNER JOIN categories 
    ON items.category_id = categories.id WHERE item_daily_views.day_stamp >= ( TO_DAYS( '$dateTo' ) -719528 -14 ) 
    AND item_daily_views.day_stamp <= ( TO_DAYS('$dateTo' ) -719528 ) GROUP BY `cat_id` ";
  


  $from = "FROM ($firstQuery) AS first INNER JOIN ($secondQuery) AS second ON first.cat_id = second.cat_id";
  
  $query = "Select $columns $from ORDER BY raport DESC";
  
  $rows = ExecuteQuery($query);
  
  return $rows;
  
}



?>
